package com.yanqu.road.dao.impl.activity.xiuxian;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.activity.xiuxian.data.XiuXianGridPositionData;
import com.yanqu.road.entity.activity.xiuxian.data.XiuXianMissionData;
import com.yanqu.road.entity.activity.xiuxian.data.XiuXianUserBehaviorData;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class XiuXianUserMissionDataDaoImpl extends TempDataObjectDao<XiuXianMissionData> {

    public static String getXiuXianMissionDataSql = "select * from `t_u_activity_xiuxian_mission_data` where `activityId`=? and `userId`=?";
    public Map<Integer, XiuXianMissionData> getXiuXianMissionData(int activityId, long userId) {
        ResultSet rs = null;
        CallableStatement call = null;
        Map<Integer, XiuXianMissionData> map = new ConcurrentHashMap<>();
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareCall(getXiuXianMissionDataSql);
            int i = 1;
            call.setInt(i++, activityId);
            call.setLong(i++, userId);
            rs = call.executeQuery();
            while (rs.next()) {
                XiuXianMissionData data = parse(rs);
                map.put(data.getMissionId(), data);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);} finally {
            getDb().closeConnection(call, rs);
        }
        return map;
    }

    public static String updateXiuXianMissionDataSql = "update `t_u_activity_xiuxian_mission_data` set missionType=?,hasExploreGridList=?,subsectionGetList=?,isFinish=? where `activityId`=? and `userId`=? and missionId=?";
    @Override
    protected boolean updateData(XiuXianMissionData xiuXianMissionData) {
        xiuXianMissionData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, xiuXianMissionData.getMissionType()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(xiuXianMissionData.getHasExploreGridList())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(xiuXianMissionData.getSubsectionGetList())));
        para.put(i++, new DbParameter(Types.BOOLEAN, xiuXianMissionData.getIsFinish()));
        para.put(i++, new DbParameter(Types.INTEGER, xiuXianMissionData.getActivityId()));
        para.put(i++, new DbParameter(Types.BIGINT, xiuXianMissionData.getUserId()));
        para.put(i++, new DbParameter(Types.INTEGER, xiuXianMissionData.getMissionId()));
        if (getDb().executeNoneQuery(updateXiuXianMissionDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String insertXiuXianMissionDataSql = "insert into `t_u_activity_xiuxian_mission_data`(activityId,userId,missionType,missionId,hasExploreGridList,subsectionGetList,isFinish) values (?,?,?,?,?,?,?)";
    @Override
    protected boolean addData(XiuXianMissionData xiuXianMissionData) {
        xiuXianMissionData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, xiuXianMissionData.getActivityId()));
        para.put(i++, new DbParameter(Types.BIGINT, xiuXianMissionData.getUserId()));
        para.put(i++, new DbParameter(Types.INTEGER, xiuXianMissionData.getMissionType()));
        para.put(i++, new DbParameter(Types.INTEGER, xiuXianMissionData.getMissionId()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(xiuXianMissionData.getHasExploreGridList())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(xiuXianMissionData.getSubsectionGetList())));
        para.put(i++, new DbParameter(Types.BOOLEAN, xiuXianMissionData.getIsFinish()));
        if (getDb().executeNoneQuery(insertXiuXianMissionDataSql, para) > 0) {
            return true;
        }
        return false;

    }

    @Override
    protected XiuXianMissionData parseData(ResultSet rs) throws SQLException {
        XiuXianMissionData xiuXianMissionData = new XiuXianMissionData();
        xiuXianMissionData.setActivityId(rs.getInt("activityId"));
        xiuXianMissionData.setUserId(rs.getLong("userId"));
        xiuXianMissionData.setMissionType(rs.getInt("missionType"));
        xiuXianMissionData.setMissionId(rs.getInt("missionId"));
        xiuXianMissionData.setHasExploreGridList(JSON.parseObject(rs.getString("hasExploreGridList"), new TypeReference<List<XiuXianGridPositionData>>() {}));
        xiuXianMissionData.setSubsectionGetList(JSON.parseObject(rs.getString("subsectionGetList"), new TypeReference<java.util.List<java.lang.Integer>>() {}));
        xiuXianMissionData.setIsFinish(rs.getBoolean("isFinish"));
        return xiuXianMissionData;
    }
}
